package top.chmin.project.cloud.common.dict.type.auto;

// field import auto
// #bind.field.dictName#

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.chmin.project.cloud.common.entity.Page;
import top.chmin.project.cloud.common.tool.DatetimeTool;
import top.chmin.project.cloud.common.tool.LocalTool;

import java.time.LocalDateTime;
import java.util.List;

/**
 * chmin
 * 2023-12-25 09:15:35
 * cloud-builder
 */
@Slf4j
@Component
public class DictTypePlugin {

    public void checkSave(DictTypeSave.Param param) {
        log.debug("checkSave-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("保存对象不能为空");
        }
    }

    public void beforeSave(DictType dictType) {
        log.debug("beforeSave-DictType: {}", JSONUtil.toJsonStr(dictType));
        dictType.setCreateBy(LocalTool.getUserId());
        dictType.setUpdateBy(LocalTool.getUserId());
        dictType.setCreateTime(LocalDateTime.now());
        dictType.setUpdateTime(LocalDateTime.now());
    }

    public void afterSave(DictTypeSave.Param param, DictType dictType, int result) {
        log.debug("afterSave-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterSave-dictType: {}", JSONUtil.toJsonStr(dictType));
        log.debug("afterSave-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkUpdateById(DictTypeUpdateById.Param param) {
        log.debug("checkUpdateById-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("更新对象不能为空");
        }
        if (param.getId() == null) {
            throw new RuntimeException("更新对象id不能为空");
        }
    }

    public void beforeUpdateById(DictType dictType) {
        log.debug("beforeUpdateById-dictType: {}", JSONUtil.toJsonStr(dictType));
        dictType.setUpdateBy(LocalTool.getUserId());
        dictType.setUpdateTime(LocalDateTime.now());
    }

    public void afterUpdateById(DictTypeUpdateById.Param param, DictType dictType, int result) {
        log.debug("afterUpdateById-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterUpdateById-dictType: {}", JSONUtil.toJsonStr(dictType));
        log.debug("afterUpdateById-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkDeleteById(DictTypeDeleteById.Param param) {
        log.debug("checkDeleteById-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("删除对象不能为空");
        }
        if (param.getId() == null) {
            throw new RuntimeException("删除对象id不能为空");
        }
    }

    public void afterDeleteById(DictTypeDeleteById.Param param, int result) {
        log.debug("afterDeleteById-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterDeleteById-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkSelectById(DictTypeSelectById.Param param) {
        log.debug("checkSelectById-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("查询对象不能为空");
        }
        if (param.getId() == null) {
            throw new RuntimeException("查询对象id不能为空");
        }
    }

    public void beforeSelectById(DictTypeSelectById.Param param, DictTypeFPC fpc) {
        log.debug("beforeSelectById-param: {}", JSONUtil.toJsonStr(param));

    }

    public void afterSelectById(DictTypeSelectById.Param param, DictTypeSelectById.Result result) {
        log.debug("afterSelectById-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterSelectById-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkSelectPage(DictTypeSelectPage.Param param) {
        log.debug("checkSelectPage-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("分页对象不能为空");
        }
    }

    public void beforeSelectPage(DictTypeSelectPage.Param param, DictTypeFPC fpc) {
        log.debug("beforeSelectPage-param: {}", JSONUtil.toJsonStr(param));
// field beforeSelectPage auto
// #bind.field.dictCode#
        fpc.dictCode().eq(param.getDictCode());
// $bind.field.dictCode$

// #bind.field.createBy#
        fpc.createBy().eq(param.getCreateBy());
// $bind.field.createBy$

// #bind.field.updateBy#
        fpc.updateBy().eq(param.getUpdateBy());
// $bind.field.updateBy$

// #bind.field.createTime#
        fpc.createTime().ge(param.getCreateTime() == null || param.getCreateTime().length == 0 ? null : param.getCreateTime()[0]);
        fpc.createTime().lt(param.getCreateTime() == null || param.getCreateTime().length < 2 ? null : DatetimeTool.nextDay(param.getCreateTime()[1]));
// $bind.field.createTime$

// #bind.field.updateTime#
        fpc.updateTime().ge(param.getUpdateTime() == null || param.getUpdateTime().length == 0 ? null : param.getUpdateTime()[0]);
        fpc.updateTime().lt(param.getUpdateTime() == null || param.getUpdateTime().length < 2 ? null : DatetimeTool.nextDay(param.getUpdateTime()[1]));
// $bind.field.updateTime$

// #bind.field.dictName#
        fpc.dictName().eq(param.getDictName());
// $bind.field.dictName$

    }

    public void afterSelectPage(DictTypeSelectPage.Param param, Page<DictTypeSelectPage.Result> result) {
        log.debug("afterSelectPage-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterSelectPage-result: {}", JSONUtil.toJsonStr(result));

    }

// entity select auto

// entity search auto
// entity search auto
public void checkSearchDictCode(DictTypeDictCodeSearch.Param param) {
    log.debug("checkSearchDictCode-param: {}", JSONUtil.toJsonStr(param));
}

    public void beforeSearchDictCode(DictTypeDictCodeSearch.Param param, DictTypeFPC fpc) {
        log.debug("beforeSearchDictCode-param: {}", JSONUtil.toJsonStr(param));
        log.debug("beforeSearchDictCode-fpc: {}", JSONUtil.toJsonStr(fpc));

    }

    public void afterSearchDictCode(DictTypeDictCodeSearch.Param param, List<DictTypeDictCodeSearch.Result> result) {
        log.debug("afterSearchDictCode-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterSearchDictCode-result: {}", JSONUtil.toJsonStr(result));

    }


}
